s2a
Arrays Timm Mar10 Sandbox Revised

Initialize an Array from a String

Problem
=======

How to quickly generate a set of key/value pairs in an array.

Solution
========

Synopsis
--------

  s2a(string, array [,separator,blankp])

Divides the input _string_ into the output _array_, separated by
_separator_ (default: ',').  Uses the odd numbered elements as the
keys and the even numbered elements as the values.  If _blankp_ is
set, then the keys and values can contain leading and trailing blanks
(otherwise, those blanks are deleted).

Note that the _array_ is completely reset by this function.

Code
----

 function s2a(str,a,  sep,blankp,  tmp,n,i) {
    sep    = sep    ? sep    : ","
    blankp = blankp ? blankp : 0
    n= split(str,tmp,sep)
    for(i=1;i<=n;i+=2) 
        if (blankp)
            a[tmp[i]]= tmp[i+1]
        else
            a[trim(tmp[i])]= trim(tmp[i+1]);
    return n/2
 }

   function S2a(   a,n,i) {
      n= s2a("abc=1,def=2,ghi=3",a,"[=,]")
	  print a["abc"] == 1
	  print a["def"] == 2
	  print a["ghi"] == 3
   }

Author
======

Tim Menzies

